<template>
  <div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '修改',
                tooltip: '修改',
                onClick: handleAdd.bind(null, CustomModalTypeEnum.Edit, record),
              },
              {
                label: '新增二级行业',
                tooltip: '新增二级行业',
                ifShow: () => record.level < 2,
                onClick: handleAdd.bind(null, CustomModalTypeEnum.AddChild, {
                  parentId: record.id,
                }),
              },
              {
                label: '删除',
                tooltip: '删除',
                onClick: handleDel.bind(null, record),
              },
              {
                label: `${dataStatusOprationEnumText[record.status]}`,
                tooltip: `${dataStatusOprationEnumText[record.status]}`,
                onClick: () => handleStatus(record),
              },
            ]"
        /></template>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleAdd(CustomModalTypeEnum.Add)">新建</a-button>
      </template>
      <template #name="{ record }">
        <p v-if="record.level === 1" style="text-align: left; padding-left: 100px">{{
          record.name
        }}</p>
        <p v-else-if="record.level === 2" style="text-align: left; padding-left: 120px">{{
          record.name
        }}</p>
        <p v-else style="text-align: left; padding-left: 140px">{{ record.name }}</p>
      </template>
      <template #status="{ record }">
        <Badge
          v-if="record.status === DataStatusEnum.Normal"
          status="success"
          :text="dataStatusEnumText[DataStatusEnum.Normal]"
        />
        <Badge v-else status="default" :text="dataStatusEnumText[DataStatusEnum.Disable]" />
      </template>
      <template #level="{ record }">
        <Tag v-if="record.level === 1" color="processing"> 一级 </Tag>
        <Tag v-else-if="record.level === 2" color="success"> 二级 </Tag>
        <Tag v-else color="gold"> 三级 </Tag>
      </template>
      <template #orgInit="{ record }">
        <Switch
          :checked="record.orgInit === OrgInitEnum.OpenOrgInit"
          @click="handleOrgInit(record)"
        ></Switch>
      </template>
    </BasicTable>
    <AddedModal @register="register" @reload="reload" />
  </div>
</template>

<script setup lang="ts">
  /**所属行业 */
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getBasicColumns } from './tableData';
  import { Badge, Modal, Tag, Switch } from 'ant-design-vue';
  import {
    CustomModalTypeEnum,
    DataStatusEnum,
    OrgInitEnum,
    dataStatusEnumText,
    dataStatusOprationEnumText,
  } from '@/enums/publicEnum';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';
  import AddedModal from './modal/added.vue';
  import {
    industryList,
    industrySwap,
    industryDel,
    industryEnable,
  } from '@/api/platform/fieldValueManage/tree/index';
  import { IndustrySector } from '@/api/platform/fieldValueManage/tree/type';

  const { createMessage } = useMessage();

  const [registerTable, { reload }] = useTable({
    title: '',
    api: industryList,
    columns: getBasicColumns(),
    rowKey: 'id',
    formConfig: {
      labelWidth: 100,
      schemas: [
        {
          field: 'keyWord',
          label: '关键词',
          component: 'Input',
          componentProps: {
            placeholder: '行业搜索',
          },
          colProps: {
            span: 5,
          },
        },
        {
          field: 'status',
          label: '状态',
          component: 'Select',
          colProps: {
            span: 5,
          },
          componentProps: {
            options: [
              { label: '全部', value: '' },
              { label: dataStatusEnumText[DataStatusEnum.Normal], value: DataStatusEnum.Normal },
              { label: dataStatusEnumText[DataStatusEnum.Disable], value: DataStatusEnum.Disable },
            ],
          },
        },
      ],
    },
    canRowDrag: true,
    beforeRowDragChange: async (source, target) => {
      try {
        await industrySwap([source.id, target.id]);
        reload();
        return false;
      } catch (error) {
        return false;
      }
    },
    useSearchForm: true,
    showTableSetting: true,
    tableSetting: { fullScreen: true },
    showIndexColumn: false,
    clickToRowSelect: false,
    pagination: {
      pageSize: 20,
    },
    actionColumn: {
      width: 150,
      title: '操作',
      fixed: 'right',
      dataIndex: 'action',
    },
    scroll: { x: 1300 },
  });

  const [register, { openModal }] = useModal();

  function handleAdd(type: CustomModalTypeEnum, row?: Partial<IndustrySector>) {
    openModal(true, {
      type,
      row,
    });
  }
  function handleDel(row: IndustrySector) {
    Modal.confirm({
      title: '提示',
      content: '该操作将删除该类目',
      onOk: async () => {
        row.id;
        await industryDel(row.id);
        createMessage.success('删除成功！');
        reload();
      },
    });
  }

  const handleStatus = async (row: IndustrySector) => {
    let status = DataStatusEnum.Normal;
    let statusMsg = '启用成功！';
    if (row.status === DataStatusEnum.Normal) {
      status = DataStatusEnum.Disable;
      statusMsg = '禁用成功！';
    } else {
      status = DataStatusEnum.Normal;
      statusMsg = '启用成功！';
    }
    await industryEnable({ id: row.id, status });
    createMessage.success(statusMsg);
    reload();
  };
  const handleOrgInit = async (row: IndustrySector) => {
    let orgInit = OrgInitEnum.OpenOrgInit;
    let orgInitMsg = '开启初始值成功！';
    if (row.orgInit === OrgInitEnum.OpenOrgInit) {
      orgInit = OrgInitEnum.CloseOrgInit;
      orgInitMsg = '关闭初始值成功！';
    } else {
      orgInit = OrgInitEnum.OpenOrgInit;
      orgInitMsg = '开启初始值成功！';
    }
    await industryEnable({ id: row.id, orgInit });
    createMessage.success(orgInitMsg);
    reload();
  };
</script>
